from datetime import datetime
from django.db import models

from django.contrib.auth.models import User

from shortsight.apps.world.models import Country
from shortsight.middleware import threadlocals


class Company(models.Model):
    """
    Data model for companies
    """
    
    name                = models.CharField(maxlength = 100)
    directors           = models.ManyToManyField(User, related_name = 'companies_directed', filter_interface = models.HORIZONTAL)
    
    time_added          = models.DateTimeField(editable = False)
    creator             = models.ForeignKey(User, related_name = 'companies_created', editable = False)
    time_modified       = models.DateTimeField(editable = False)
    editor              = models.ForeignKey(User, related_name = 'companies_modified', editable = False)
    
    
    class Meta:
        db_table            = 'companies_company'
        verbose_name        = 'company'
        verbose_name_plural = 'companies'
        ordering            = ('-time_added',)
        
        
    class Admin:
        pass
        
        
    def __unicode__(self):
        return self.name
        
        
    def save(self):
        now, user = datetime.now(), threadlocals.get_current_user()
        if not self.id:
            self.time_added, self.creator = now, user
        self.time_modified, self.editor = now, user
        super(Company, self).save()


    
class Department(models.Model):
    """
    Data model for organisational departments
    """
    company             = models.ForeignKey(Company, raw_id_admin = True)
    name                = models.CharField(maxlength = 100)
    managers            = models.ManyToManyField(User, related_name = 'departments_managed', filter_interface = models.HORIZONTAL)
    
    time_added          = models.DateTimeField(editable = False)
    creator             = models.ForeignKey(User, related_name = 'departments_created', editable = False)
    time_modified       = models.DateTimeField(editable = False)
    editor              = models.ForeignKey(User, related_name = 'departments_modified', editable = False)
    
    
    class Meta:
        db_table            = 'companies_departments'
        verbose_name        = 'department'
        verbose_name_plural = 'departments'
        ordering            = ('-time_added',)
        
        
    class Admin:
        pass
    
    
    def __unicode__(self):
        return '%s (%s)' % (self.name, self.company)
        
        
    def save(self):
        now, user = datetime.now(), threadlocals.get_current_user()
        if not self.id:
            self.time_added, self.creator = now, user
        self.time_modified, self.editor = now, user
        super(Department, self).save()



class Office(models.Model):
    """
    Data models for company offices
    """
    company             = models.ForeignKey(Company, raw_id_admin = True)
    department          = models.ForeignKey(Department, null = True, blank = True, raw_id_admin = True)
    
    address_line1       = models.CharField(maxlength = 75, blank = True)
    addres1_line2       = models.CharField(maxlength = 75, blank = True)
    city                = models.CharField(maxlength = 75, blank = True)
    state               = models.CharField(maxlength = 75, blank = True)
    postal_code         = models.CharField(maxlength = 75, blank = True)
    country             = models.ForeignKey(Country)
    
    url                 = models.URLField(blank = True)
    email               = models.EmailField(blank = True)
    phone               = models.CharField(maxlength = 20, blank = True)
    fax                 = models.CharField(maxlength = 20, blank = True)
    
    time_added          = models.DateTimeField(editable = False)
    creator             = models.ForeignKey(User, related_name = 'offices_created', editable = False)
    time_modified       = models.DateTimeField(editable = False)
    editor              = models.ForeignKey(User, related_name = 'offices_modified', editable = False)
    
    class Meta:
        db_table            = 'companies_offices'
        verbose_name        = 'office'
        verbose_name_plural = 'offices'
        
        
    class Admin:
        pass
        
    
    def __unicode__(self):
        return "Office info for %s" % self.company
        
        
    def save(self):
        now, user = datetime.now(), threadlocals.get_current_user()
        if not self.id:
            self.time_added, self.creator = now, user
        self.time_modified, self.editor = now, user
        super(Office, self).save()
    

    
class Employee(models.Model):
    """
    Data model for company employees
    """
    company             = models.ForeignKey(Company, raw_id_admin = True)
    user                = models.ForeignKey(User, raw_id_admin = True)
    
    email               = models.EmailField()
    work_phone          = models.CharField(maxlength = 20, blank = True)
    work_phone_ext      = models.CharField(maxlength = 4, blank = True)
    mobile              = models.CharField(maxlength = 20, blank = True)
    home_phone          = models.CharField(maxlength = 20, blank = True)
    
    im_aol              = models.CharField(maxlength = 75, blank = True)
    im_google           = models.CharField(maxlength = 75, blank = True)
    im_icq              = models.CharField(maxlength = 75, blank = True)
    im_jabber           = models.CharField(maxlength = 75, blank = True)
    im_msn              = models.CharField(maxlength = 75, blank = True)
    im_yahoo            = models.CharField(maxlength = 75, blank = True)
    
    mugshot             = models.ImageField(upload_to = 'companies/employees/mugshots', blank = True)
    
    time_added          = models.DateTimeField(editable = False)
    creator             = models.ForeignKey(User, related_name = 'employees_created', editable = False)
    time_modified       = models.DateTimeField(editable = False)
    editor              = models.ForeignKey(User, related_name = 'employees_modified', editable = False)
    
    
    class Meta:
        db_table            = 'companies_employee'
        verbose_name        = 'employee'
        verbose_name_plural = 'employees'
        
        
    class Admin:
        pass
        
        
    def __unicode__(self):
        return "%s (employee od %s)" % (self.user.get_full_name, self.company)
        
        
    def save(self):
        now, user = datetime.now(), threadlocals.get_current_user()
        if not self.id:
            self.time_added, self.creator = now, user
        self.time_modified, self.editor = now, user
        super(Employee, self).save()
